var util = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
var page = 1;
var hasMore = true;
var loadding = false;
var messagetmp = '';

var screnn_w = 320;

Page({
  data: {
      util: util,
      limit: 10,
      searchVal:"",
      huiyuan:0,
      searchResult:[],
      content_item_w: 150,
      colorSet:'',
    
  },
  
  onLoad: function(data){
    // uid = data.uid || "";
    page = 1;
    hasMore = true;
    loadding = false;

    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        screnn_w = res.windowWidth;
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screnn_w: res.windowWidth,
          content_w: res.windowWidth - 78,
          goodTypeView_w: (res.windowWidth - 78) / 3
        });
      }
    })
    
    //获取颜色配置
    app.getColorSetData(function (color) {
      var col = color.goods_col ? parseInt(color.goods_col) : 3;
      col = col > 0 ? col : 3;
      that.setData({
        colorSet: color,
        content_item_w: (that.data.content_w - (col + 1) * 5) / col,
        goodTypeView_w: screnn_w / 3
      })
      that.resetNavColor();
    });
    
    var val = data.val;
    this.setData({
      huiyuan: data.huiyuan
    })
    if (val) {
      this.setData({
        searchVal: val
      })
    }
    this.load();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    //获取颜色配置
    app.getColorSetData(function (color) {
      var col = color.goods_col ? parseInt(color.goods_col) : 3;
      col = col > 0 ? col : 3;
      that.setData({
        colorSet: color,
        content_item_w: (screnn_w - (col + 1) * 7) / col
      })
      that.resetNavColor();
    });
  },
  //设置titlebar 样式
  resetNavColor: function () {
    var bg_color = '#f0f0f0';
    var fg_color = '#000000';
    if (this.data.colorSet) {
      bg_color = this.data.colorSet.titlebg;
      fg_color = this.data.colorSet.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);
    var path = e.detail.value.nav_path;
    if (path && path.indexOf('navto:') == 0) {
      path = path.slice(6);
    }
    if (path) {
      wx.reLaunch({
        url: path,
      })
    }
  },

  loadMore: function () {
    
    if (hasMore && !loadding) {
      page++;
      
      this.load();
    }
  },
  inputVal: function (e) {
    this.setData({
      searchVal: e.detail.value
    })
  },

  load:function(){
    loadding = true;
    var that = this;
    
    var params = {
      publickey: util.publiccode,
      ac: "search",
      orgid: util.orgid,
      topictype: 4,
      keyword: that.data.searchVal,
      type: 1,
      page: page,
      num: that.data.limit
    };
    console.log("search请求参数：", params);
    util.api({
      data: params,
      success: function (responseData) {
        console.log(responseData);
        var resData = responseData.data;
        var newData;
        if (resData.msg) {
          that.setData({
            hasMore: false
          });
          if(page <= 1){
            that.setData({
              searchResult: [],
            });
          }
          wx.showToast({
            title: '没有符合条件的数据',
          })
        } else {
          resData.forEach(function (n, j) {
            n.message = app.parseHTMLText(n.message);
            var nodes = WxParse.getImgurls('detailMessage', 'html', n.message, that, 12).nodes;
            console.log("nodes:", nodes)

            that.whileNodes(nodes);
            console.log(messagetmp)
            n.message = messagetmp;
            messagetmp = ''
          })
          if (page <= 1) {
            that.setData({
              searchResult: resData,
            });
          } else {
            newData = that.data.searchResult.concat(resData);
            that.setData({
              searchResult: newData,
            });
          }
          loadding = false
        }
      }
    });
  },
  goAtherPage: function(e){
      var url = e.currentTarget.dataset.link;
      wx.redirectTo({url: url})
  },
  search: function(){
    page = 1;
    this.load();
  },


  whileNodes: function (n) {
    var that = this
    for (let i = 0; i < n.length; i++) {
      if (n[i].node && n[i].node == "text") {
        console.log("text:", n[i].text)
        messagetmp += n[i].text
        return;
      }
      if (n[i].nodes) {
        console.log("whileNodes:", n[i].nodes)
        that.whileNodes(n[i].nodes);
      }
    }

  },
})